package thread.test4Queue;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * 基于阻塞队列的生产者，消费者
 */
public class Test3ProduceConsumer {

    public static void main(String[] args) throws InterruptedException {
        final ArrayBlockingQueue<Integer> queue=new ArrayBlockingQueue<Integer>(1,true);

        //第一线程，生产者
       Thread t1= new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for(int i=0;i<100;i++){
                        System.out.println("producer produce"+i);
                        queue.put(i);
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();

        //第二个线程，消费者
        Thread t2=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    for(int i=0;i<100;i++){
                        System.out.println("comsumer comsumer"+queue.take());
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t2.start();

        Thread.sleep(5000);
    }
}
